package com.chenyi.web.controller.user;

import java.util.List;

import com.chenyi.user.service.ISysUserService;
import com.chenyi.common.core.domain.entity.SysAdmin;
import com.chenyi.common.core.domain.entity.SysUser;
import com.chenyi.common.utils.AdminUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import com.chenyi.common.annotation.Log;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.utils.poi.ExcelUtil;
import com.chenyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author wangkh
 * @date 2020-10-11
 */
@RestController
@RequestMapping("/admin/user")
public class AdminUserController extends BaseController
{
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@admin.hasPermi('system:user:list')")
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(SysUser sysUser)
    {
        startPage();
        List<SysUser> list = sysUserService.selectUserList(sysUser);
        return new ResponseEntity<>(getDataTable(list),HttpStatus.OK);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@admin.hasPermi('system:user:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<Object> export(SysUser sysUser)
    {
        List<SysUser> list = sysUserService.selectUserList(sysUser);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@admin.hasPermi('system:user:query')")
    @GetMapping(value = "/{userId}")
    public ResponseEntity<Object> getInfo(@PathVariable("userId") Long userId)
    {
        return ResponseEntity.ok(sysUserService.selectUserById(userId));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@admin.hasPermi('system:user:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Object> add(@RequestBody SysUser sysUser)
    {
        sysUserService.insertUser(sysUser);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@admin.hasPermi('system:user:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Object> edit(@RequestBody SysUser sysUser)
    {
        sysUserService.updateUser(sysUser);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@admin.hasPermi('system:user:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] userIds)
    {
        sysUserService.deleteUserByIds(userIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@admin.hasPermi('system:admin:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public ResponseEntity<Object> changeStatus(@RequestBody SysUser user)
    {
        user.setUpdateBy(AdminUtils.getUsername());
        sysUserService.updateUserStatus(user);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@admin.hasPermi('system:admin:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public ResponseEntity<Object> resetPwd(@RequestBody SysUser user)
    {
        user.setPassword(AdminUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(AdminUtils.getUsername());
        sysUserService.resetPwd(user);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
